package base.ifunction;

import java.util.function.Function;

public class TestFunction {
    public static void main(String[] args) {
        testAndThen();
    }

    private static void testStaticApply() {
        Function<Integer, String> intToString = (Integer num) -> String.valueOf(num);
//        Function<Integer, String> intToString = String::valueOf;
        System.out.println(intToString.apply(5));
    }

    private static final class MyObject {
        public String myMethod(String param) {
            return "Hello, " + param;
        }
    }

    private static void testObjectApply() {
        var obj = new MyObject();
        Function<String, String> intToString = (String num) -> obj.myMethod(num);
//        intToString = obj::myMethod;
        System.out.println(intToString.apply("5"));
    }

    private static void testCompose() {
        Function<Integer, Integer> times2 = i -> i * 2;
        Function<Integer, Integer> squared = i -> i + 1;

        //先squared --> 后times2
        System.out.println(times2.compose(squared).apply(2));
    }

    private static void testAndThen() {
        Function<Integer, Integer> times2 = i -> i * 2;
        Function<Integer, Integer> squared = i -> i + 1;

        //先times2 --> 后squared
        System.out.println(times2.andThen(squared).apply(2));
    }
}
